<?php

namespace App\Console\Crons;

use App\Datalog\AdFirstup;
use App\Models\FirstupAd;
use App\Models\Manager\AdToutiaoMaterial;

class AdFirstupMatchToutiaoMaterialCron extends CronBase
{
    public function run($stime)
    {
        $end_time   = $stime + 86400;
        $start_time = $stime - 86400;
        $query      = FirstupAd::whereRaw("create_time>={$start_time} AND create_time<{$end_time} AND media_id = 1002 AND monitor_id>1 AND mid3 != '' AND material_id = ''");
        $query->chunk(10000, function ($list) {
            $mid3_arr       = array_unique(array_column($list->toArray(), 'mid3'));
            $material_list = AdToutiaoMaterial::whereIn('material_id', $mid3_arr)->get()->keyBy('material_id')->toArray();
            foreach ($list as $val) {
                if (!empty($val->material_md5)) continue;
                if (!isset($material_list[$val->mid3])) continue;
                $val->material_id  = $material_list[$val->mid3]['ad_material_id'];
                $val->material_md5 = $material_list[$val->mid3]['file_md5'];
                $val->save();

                AdFirstup::getInstance()->addLog($val->id);
            }
        });
    }
}
